/*
 * ========THE SOLMIX PROJECT=====================================
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * http://www.gnu.org/licenses/ 
 * or see the FSF site: http://www.fsf.org. 
 */
package org.solmix.web.security;

import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.config.Ini;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.util.CollectionUtils;
import org.apache.shiro.web.config.IniFilterChainResolverFactory;


/**
 * 
 * @author solomon
 * @version $Id$  2011-6-5
 */

public class ShiroConfigIniManager
{
   private SecurityManager securityManager;

   private Map<String, Filter> filters;

   private Map<String, String> filterChainDefinitionMap; //urlPathExpression_to_comma-delimited-filter-chain-definition

   private String loginUrl;
   private String successUrl;
   private String unauthorizedUrl;
   
  
   
   /**
    * @return the securityManager
    */
   public SecurityManager getSecurityManager()
   {
      return securityManager;
   }


   
   /**
    * @param securityManager the securityManager to set
    */
   public void setSecurityManager(SecurityManager securityManager)
   {
      this.securityManager = securityManager;
   }


   
   /**
    * @return the filters
    */
   public Map<String, Filter> getFilters()
   {
      return filters;
   }


   
   /**
    * @param filters the filters to set
    */
   public void setFilters(Map<String, Filter> filters)
   {
      this.filters = filters;
   }


   
   /**
    * @return the filterChainDefinitionMap
    */
   public Map<String, String> getFilterChainDefinitionMap()
   {
      return filterChainDefinitionMap;
   }


   
   /**
    * @param filterChainDefinitionMap the filterChainDefinitionMap to set
    */
   public void setFilterChainDefinitionMap(Map<String, String> filterChainDefinitionMap)
   {
      this.filterChainDefinitionMap = filterChainDefinitionMap;
   }


   
   /**
    * @return the loginUrl
    */
   public String getLoginUrl()
   {
      return loginUrl;
   }


   
   /**
    * @param loginUrl the loginUrl to set
    */
   public void setLoginUrl(String loginUrl)
   {
      this.loginUrl = loginUrl;
   }


   
   /**
    * @return the successUrl
    */
   public String getSuccessUrl()
   {
      return successUrl;
   }


   
   /**
    * @param successUrl the successUrl to set
    */
   public void setSuccessUrl(String successUrl)
   {
      this.successUrl = successUrl;
   }


   
   /**
    * @return the unauthorizedUrl
    */
   public String getUnauthorizedUrl()
   {
      return unauthorizedUrl;
   }


   
   /**
    * @param unauthorizedUrl the unauthorizedUrl to set
    */
   public void setUnauthorizedUrl(String unauthorizedUrl)
   {
      this.unauthorizedUrl = unauthorizedUrl;
   }


   public void setFilterChainDefinitions(String definitions) {
      Ini ini = new Ini();
      ini.load(definitions);
      //did they explicitly state a 'urls' section?  Not necessary, but just in case:
      Ini.Section section = ini.getSection(IniFilterChainResolverFactory.URLS);
      if (CollectionUtils.isEmpty(section)) {
          //no urls section.  Since this _is_ a urls chain definition property, just assume the
          //default section contains only the definitions:
          section = ini.getSection(Ini.DEFAULT_SECTION_NAME);
      }
      setFilterChainDefinitionMap(section);
  }

}
